<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>GrPPI: Stream iteration pattern</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">0.2</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Stream iteration pattern </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <b>stream iteration</b> pattern allows loops in data stream processing. An operation is applied to a data item until a predicate is satisfied. When the predicate is met, the result is sent to the output stream.</p>
<p>The interface to the <b>stream iteration</b> pattern is provided by function <code><a class="el" href="group__stream__iteration__pattern__native.html#gaf495b00aac103ff5a2d3fa708b06c5e2" title="Invoke Stream iteration pattern on a data stream with native parallel execution with a generator...">grppi::repeat_until()</a></code>. As all functions in <em>GrPPI</em>, this function takes as its first argument an execution policy.</p>
<div class="fragment"><div class="line"><a class="code" href="group__stream__iteration__pattern__native.html#gaf495b00aac103ff5a2d3fa708b06c5e2">grppi::repeat_until</a>(exec, other_arguments...);</div></div><!-- fragment --><h2>Stream iteration variants</h2>
<p>There is a single variant:</p>
<ul>
<li><em>Stand-alone stream iteration</em>: Applies iteration to a stream of values produced by a generator and invokes a consumer for each value that satisfies the iteration predicate.</li>
</ul>
<p><b>Note</b>: Although there is a single variant of stream iteration, the transformation applied in the iteration loop may be some other composable pattern (<b>farm</b> or <b>pipeline</b>).</p>
<h2>Key elements in stream iteration</h2>
<p>The key elements in a <b>stream iteration</b> are a <b>Transformer</b> used to transform data items, and a <b>Predicate</b> that defines when the iteration should finish.</p>
<p>The <b>Transformer</b> may be any C++ callable entity that takes a data item and applies a transformation to it. Thus, a <b>Transformer</b> <code>op</code> is any operation that, given an input value <code>x</code> of type <code>T</code>makes valid the following:</p>
<div class="fragment"><div class="line">U res{transformer(x)};</div></div><!-- fragment --><p>The <b>Predicate</b> may be any C++ callable entity that takes a data item and returns a value that is contextually convertible to <code>bool</code>. Thus, a predicate <code>pred</code> is any operation, that given a value <code>x</code> of type <code>T</code>, makes the following valid:</p>
<div class="fragment"><div class="line"><span class="keywordflow">do</span> { <span class="comment">/*...*/</span> } <span class="keywordflow">while</span> (!predicate(item));</div></div><!-- fragment --><p>A stand-alone <b>stream iteration</b> also has a <b>Generator</b>. The generator may be any C++ callable entity that produces values in a way that allows to signal an indication of end-of stream. For this purpose, the <b>stream iteration</b> requires that the generator produces values of any type that is compatible with a subset of an optional interface:</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> r = generator(); <span class="comment">// r is a value generated by a generator.</span></div><div class="line"><span class="keywordflow">if</span> (r) {       <span class="comment">// r is convertible to bool</span></div><div class="line">  <span class="keyword">auto</span> y = *r; <span class="comment">// r can be dereferenced.</span></div><div class="line">}</div></div><!-- fragment --><p>Additionally, a filter may have a <b>Consumer</b>. The consumer may be any C++ callable entity that takes values of the result type of the <b>Transformer</b>.</p>
<h2>Details on stream iteration variants</h2>
<h3>Stand-alone stream iteration</h3>
<p>A stand alone <b>stream iteration</b> has four elements:</p>
<ul>
<li>A <b>Generator</b> of values.</li>
<li>A <b>Transformer</b> of values.</li>
<li>A <b>Predicate</b> for terminating the iteration.</li>
<li>A <b>Consumer</b> of generated values. <hr/>
 <b>Example</b>: For every natural number x, print the first value x*2^n that is greater than 1024. <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;grppi::repeat_until(ex</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;  [i=0,max=100]() mutable -&gt; optional&lt;int&gt; {</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;    if (i&lt;max) return i++;</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;    else return {};</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;  },</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;  [](int x) { return 2*x; },</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;  [](int x) { return x&gt;1024; },</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;  [](int x) { cout &lt;&lt; x &lt;&lt; endl; }</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;);</div></div><!-- fragment --><hr/>
 </li>
</ul>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>
